package org.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.example.Dto.DishDTO;
import org.example.comment.R;
import org.example.entity.Category;
import org.example.entity.Dish;
import org.example.service.DishFlavorService;
import org.example.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private DishFlavorService dishFlavorService;


    /**
     * 保存菜品信息
     */
    @PostMapping
    public R<String> save(@RequestBody DishDTO dishDTO) {
        log.info(dishDTO.toString());
        dishService.saveWithFlavor(dishDTO);
        return R.success("新增菜品成功");
    }


    /**
     * 获取菜品信息
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize,String  name) {
        Page<Dish> pageInfo = new Page<>(page, pageSize);
        Page<DishDTO> dtoPageInfo = new Page<>();
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null, Dish::getName, name);
        queryWrapper.orderByDesc(Dish::getUpdateTime);
        dishService.page(pageInfo, queryWrapper);
        //对象拷贝
        BeanUtils.copyProperties(pageInfo, dtoPageInfo, "records");
        //菜品分类获取
        List<DishDTO> list = dishService.listWithFlavor(pageInfo.getRecords());
        dtoPageInfo.setRecords(list);
        return R.success(dtoPageInfo);
    }

    /**
     * 修改菜品
     */
    @GetMapping("/{id}")
    public R<DishDTO> get(@PathVariable Long id) {
        DishDTO dishDTO = dishService.getByIdWithFlavor(id);
        return R.success(dishDTO);
    }

    /**
     * 修改菜品
     */
    @PutMapping
    public R<String> update(@RequestBody DishDTO dishDTO) {
        log.info("修改分类信息：{}", dishDTO);
        dishService.updateWithFlavor(dishDTO);
        return R.success("修改成功");
    }

    /**
     * 修改商品的状态
     */
    @PostMapping("/status/{status}")
    public R<String> updateStatus(@PathVariable Integer status, Long[] ids) {
        log.info("修改商品状态：{}", status);
        for (Long id : ids) {
            Dish dish = new Dish();
            dish.setId(id);
            dish.setStatus(status);
            dishService.updateById(dish);
        }
        return R.success("修改成功");
    }

    /**
     * 删除商品
     */
    @DeleteMapping
    public R<String> delete(Long[] ids) {
        log.info("删除商品：{}", ids);
        for (Long id : ids) {
            dishService.removeById(id);
        }
        return R.success("删除成功");
    }

    /**
     * 新建套餐，查询菜品数据
     */
    @GetMapping("/list")
    public R<List<Dish>> list(Dish dish) {
        log.info("查询条件：{}", dish);
        //构造查询条件
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(dish.getCategoryId() != null, Dish::getCategoryId, dish.getCategoryId());
        //添加过滤条件 这里的1是起售状态
        queryWrapper.eq(Dish::getStatus, 1);
        //添加排序条件
        queryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
        List<Dish> list = dishService.list(queryWrapper);
        return R.success(list);
    }




}
